package com.landtool.lanbase.database.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.landtool.lanbase.constant.CacheConsts;
import com.landtool.lanbase.elasticsearch.RestClientUtils;
import com.landtool.lanbase.entity.ShipEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    private static Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisTemplate <String,Object> redisTemplate;

    @RequestMapping("/getTargetById")
    @ResponseBody
    public String getTargetById(int id) {
        String A="";
        String newKey = CacheConsts.openProvince+"_new";

        List<ShipEntity> list=new ArrayList <>();
        if(redisTemplate.hasKey(newKey)){
            list = (List <ShipEntity>) redisTemplate.opsForValue().get(newKey);
        }
        Gson gson = new GsonBuilder().serializeNulls().create();
        try {
            for (int i = 0; i < list.size(); i++) {
                ShipEntity shipEntity = list.get(i);
                if(shipEntity.getTarget_id()==id){
                    A="{\"code\":0,\"msg\":\"操作成功\",\"data\":"+gson.toJson(shipEntity)+"}";
                }
            }
        }catch (NullPointerException e){
            log.error("程序没有获取到数据");
        }
        return A;
    }

}
